import { Module } from "vuex";
// import { getGetDict } from '@/api/common'
import { getResource } from "@/api/resource";

export interface TerminalState {
  data: {
    key: string;
    name: string;
  }[];
}

// eslint-disable-next-line
const cmProjectType: Module<TerminalState, any> = {
  namespaced: true,
  state: () => ({
    data: [],
  }),
  getters: {
    getCmProjectType(state) {
      return state.data;
    },
    getName(state) {
      // debugger
      return (key: string) => state.data.find((item) => item.key === key)?.name;
    },
  },
  mutations: {
    SET_CM_PROJECT_TYPE(state, payload) {
      state.data = payload;
    },
  },
  actions: {
    getDict({ commit }) {
      return new Promise((resolve, reject) => {
        getResource("CM_PROJECT_TYPE")
          .then((res) => {
            if (res && res.data) {
              const list = res.data.properties.meta;
              commit("SET_CM_PROJECT_TYPE", list);
              resolve(res);
            } else {
              reject(res);
            }
          })
          .catch(reject);
      });
    },
  },
};

export default cmProjectType;
